const { resolve } = require('path')

const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { VueLoaderPlugin } = require('vue-loader/dist/index.js')

const { getGlobalEnv, setEnv } = require('../build/env.js')

// 配置基础环境变量
setEnv()

const baseConfig = {
  entry: resolve(__dirname, '../src/main.js'),
  output: {
    path: resolve(__dirname, '../dist'),
    filename: 'js/[name].js',
    chunkFilename: 'js/[name]_chunk.js'
  },
  commonRules: [
    {
      test: /\.js$/,
      exclude: /node_modules/,
      loader: 'babel-loader',
    },
    {
      test: /\.(jpe?g|png|svg|gif)/,
      type: 'asset/resource',
    },
    {
      test: /\.(woff|woff2|eot|ttf|otf)$/i,
      type: 'asset/resource',
    },
  ],
  commonPlugins: [
    new HtmlWebpackPlugin({
      template: resolve(__dirname, '../public/index.html'),
      // 压缩html代码
      minify: {
        // 移除空格
        collapseWhitespace: true,
        // 移除注释
        removeComments: true
      }
    }),
    new VueLoaderPlugin(),
    new webpack.DefinePlugin({
      'process.env': {
        ...getGlobalEnv()
      }
    })
  ],
  commonResolve: {
    alias: {
      '@': resolve(__dirname, '../src')
    },
    extensions: ['.js', '.json', '.vue', '.ts', '.jsx']
  },
  commonOptimization: {
    splitChunks: {
      chunks: 'all',
      name: 'vendors',
      cacheGroups: {
        vendors: {
          test: /[\\/]node_modules[\\/]/,
          reuseExistingChunk: true,
          priority: -10
        }
      }
    }
  },
  proxy: {}
}

module.exports = baseConfig;
